package search;

import util.RandomUtil;

import java.util.Arrays;

public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = RandomUtil.randomArr();
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
        BinarySearch binarySearch = new BinarySearch();
        int c = binarySearch.search(arr[arr.length - 3], arr);
        System.out.println(c);
    }

    public int search(int val, int[] arr) {
        int l = 0;//左索引
        int r = arr.length - 1;//右索引

        while (l <= r) {
            System.out.println("l:" + l + ",r:" + r);
            int mid = l + (r - l) / 2;
            if (arr[mid] == val) {
                return mid;
            } else if (arr[mid] > val) {
                r = mid - 1;

            } else {
                l = mid + 1;
            }


        }
        return -1;
    }
}
